package com.grantha.controllers;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.grantha.util.DB;
import com.grantha.util.Util;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.grantha.util.MD5Generator;

/**
 * Servlet implementation class login2servlet
 */
@WebServlet("/login2servlet")
public class login2servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger log = Logger.getLogger(this.getClass());
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login2servlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		HttpSession session =  request.getSession();
		PrintWriter out = response.getWriter();
		
		log.info("Servlet Stated");
		//coding start 
		
		String username = request.getParameter("username"); 
		String password = MD5Generator.md5(request.getParameter("password"));
		
		
		log.info("username="+username);
		log.info("password="+password);
		
		if((username.equals(""))||(password.equals("d41d8cd98f00b204e9800998ecf8427e"))){
			out.print(Util.sendJSONMessage(false, "User Name and Password must be filled out"));
			
		}
		else{
		
		try {
			
			ResultSet rs1= DB.search("select * from users where username=? and password=?",username,password );
			if(rs1.next()){
				username = rs1.getString("username");
				session.setAttribute("CurrentUserName",username);
				session.setAttribute("CurrentUserRights", "Operator");
				RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
				rd.include(request, response);
				
				//out.print("success");
				log.info("success");
				
			}else{
				//out.print(Util.sendJSONMessage(false, "User Name and Password doesn't match"));
				session.setAttribute("CurrentUserName"," ");
				session.setAttribute("CurrentUserRights", " ");
				RequestDispatcher rd = request.getRequestDispatcher("jsp/login.jsp");
				rd.include(request, response);
				//out.print("not found");
				log.info("not found");
			}
			
			
		} catch (Exception e) {
			out.print("error");
			log.error("Error");
		}
		}
		
		//coding ends
		
		out.close();
		log.info("Servlet end");
	}

	
}
	


